package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketAddress;
import com.cskaoyan.app.bean.po.MarketAddressExample;
import com.cskaoyan.app.mapper.MarketAddressMapper;
import com.cskaoyan.app.wxservice.WxAddrService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WxAddrServiceImpl implements WxAddrService {

    @Autowired
    private MarketAddressMapper addressMapper;

    @Override
    public CommonData<MarketAddress> getAddrByUserId(Integer userid) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userid).andDeletedEqualTo(false);
        List<MarketAddress> addresses = addressMapper.selectByExample(example);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(addresses);
        return CommonData.data(pageInfo);
    }

    @Override
    @Transactional
    public void saveAddr(MarketAddress address) {
        if (address == null) {
            return;
        }
        Integer id = address.getId();
        Boolean isDefault = address.getIsDefault();
        // 如果当前为默认地址，则将此人其他地址都设为非默认
        if (isDefault) {
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(address.getUserId());
            MarketAddress record = new MarketAddress();
            record.setIsDefault(false);
            addressMapper.updateByExampleSelective(record, example);
        }
        // id没传表示新增，传值表示更新
        if (id == null || id == 0) {
            address.setId(null);
            address.setAddTime(new Date());
            address.setUpdateTime(new Date());
            addressMapper.insertSelective(address);
        } else {
            address.setDeleted(false);
            address.setUpdateTime(new Date());
            addressMapper.updateByPrimaryKey(address);
        }
    }

    @Override
    public void deleteById(Integer id) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        MarketAddress record = new MarketAddress();
        record.setDeleted(true);
        addressMapper.updateByExampleSelective(record, example);
    }

    @Override
    public MarketAddress getDetailById(Integer id) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketAddress> addresses = addressMapper.selectByExample(example);
        return addresses.stream().findFirst().orElse(null);
    }
}
